import { connect } from '@umijs/max'
import { notification, Spin } from 'antd'
import { useEffect } from 'react'
import { queryLoginCallback } from './api'
import styles from './index.less'
import { getHashParameterByName } from '@/utils/helper'

notification.config({
  maxCount: 3,
})

const LoginWithYunqiao = (props) => {
  useEffect(() => {
    const ticketStr = getHashParameterByName('ticket')
    fetchData(ticketStr)
  }, [])
  async function fetchData(ticketStr) {
    // 调取后云巧端接口生成cookie
    const res = await queryLoginCallback({
      ticket: ticketStr || '',
    })
    const { status, data = {}, msg = '操作失败' } = res
    if (Number(status) === 200) {
      localStorage.setItem('loginSta', '1')
      const redirect = getHashParameterByName('redirect')
      const defaultWorkSpace = sessionStorage.getItem('defaultWorkSpace')
      if (!redirect || redirect === '/Login' || !defaultWorkSpace) {
        window.location = `${window.suanpan}#/workSpace`
      } else {
        window.location = `${window.suanpan}#${redirect}`
      }
    } else if (Number(status) === 402) {
      const hash = window.location.hash?.split('#')?.[1]
      const backURL = encodeURIComponent(`${window.location.origin + window.location.pathname}#/login?redirect=${hash}`)
      // window.location = `http://sf-main.yunqiao.ingress.dxsp.tech/sso/login?service=${backURL}`
      window.location = `http://sf-api-gateway-xltest.xuelang-test.ingress.dxsp.tech/sso/login?service=${backURL}`
    }
  }

  return (
    <div className={styles.login_loading} loading="true">
      <div className={styles.login_loading_content}>
        <span>跳转中</span>
        <Spin />
      </div>
    </div>
  )
}

export default connect(({ login }) => ({ ...login }))(LoginWithYunqiao)
